// ActionScript file
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;


/**
 * 发送HTTPService 
 * @param String url
 * @param Object param
 * @param Function Callback
 * 
 */
public function SendHttpService( url:String, param:Object = null, Callback:Function = null, name:String='' ):void
{
	var httpServiceObj:HTTPService = new HTTPService();
	httpServiceObj.resultFormat = "e4x";
	httpServiceObj.method = "POST";
	httpServiceObj.url = url;
	httpServiceObj.addEventListener(FaultEvent.FAULT, function(event:FaultEvent):void{doFault(event,name)} );
	httpServiceObj.addEventListener(ResultEvent.RESULT, Callback );
	httpServiceObj.send(param);
}

/**
 *  do fault for HTTPService
 */
private function doFault(event:FaultEvent, name:String):void{
	trace(name+' is Fault.');
	trace(event.message);
	//Alert.show(name+' is Fault.');
	//event.target.sent();
	//this.process1.label = name+'加载失败。 '+this.countForLoad;
}


private function getConfigRPCResult(event:ResultEvent):void
{
	trace(event.result);
	var config:XML = XML(event.result);
	this.SITENAME = config.siteurl;
	this.stage_id = config.stage_id;
	this.border_id = config.border_id;
	this.jewelry_id = config.jewelry_id;
	this.calendar_id = config.calendar_id; 
	mling.setLab('加载配置文件.完成');
	
	mling.setLab('读取用户照片');
	this.SendHttpService(this.url_MyPhotoRPC, null, this.getMyPhotoRPCResult, 'Photo');
	
	this.SendHttpService(this.URLGetMenuRPC, {mid:this.stage_id},this.StagesMenuRPCResult, 'StageMenu');
	this.SendHttpService(this.URLGetMenuRPC, {mid:this.border_id},this.BorderMenuRPCResult, 'borderMenu');
	this.SendHttpService(this.URLGetMenuRPC, {mid:this.jewelry_id},this.JewelryMenuRPCResult, 'jewelryMenu');
	this.SendHttpService(this.URLGetMenuRPC, {mid:this.calendar_id},this.CalendarMenuRPCResult, 'calendarMenu');
	
	//删除初始化屏幕 x
    this.initTotal -= 1;
    trace(this.initTotal);
	if(this.initTotal == 0){
		this.removeChild(this.mling);
	}
}


/**
 * 读取用户上传的图片
 */
private function getMyPhotoRPCResult(event:ResultEvent):void{
	//trace(event.result);
	var tmp:Array = new Array();
	MyUploadPhotos.removeAll();
	for each (var s:XML in event.result.info)
	{
		MyUploadPhotos.addItem({name: s.name, urlx: s.urlx,width:s.width, height:s.height, idx:s.idx, thumb:s.thumb});
	}
	
	trace('用户照片加载完毕');
	mling.setLab('读取用户照片 完成');
	
	mling.setLab('读取已完成作品');
	this.SendHttpService(this.url_MyMakedRPC, null,this.MakedRPCResult, 'Maked');
	
	
	
	//删除初始化屏幕 y
    this.initTotal -= 1;
    trace(this.initTotal);
	if(this.initTotal == 0){
		this.removeChild(this.mling);
	}
}

/**
 * Maked HTTPService成功
 * 
 */
private function MakedRPCResult(event:ResultEvent):void
{
	trace(event.result);
	mymaked.removeAll();
	for each (var s:XML in event.result.info){
		mymaked.addItem({name: s.name, urlx: s.urlx,urls: s.urls, width:s.width, height:s.height, idx:s.idx, thumb:s.thumb, size:s.size});
		//trace(s);
	}
	trace('用户作品加载完毕');
	mling.setLab('读取已完成作品 完成');
	//this.removeChild(this.mling);
	//删除初始化屏幕 z
    this.initTotal -= 1;
    trace(this.initTotal);
	if(this.initTotal == 0){
		this.removeChild(this.mling);
	}
}

// for Stages HTTPService
private function StagesMenuRPCResult(event:ResultEvent):void
{
	stageXML = XML(event.result);
	trace('场景分类菜单加载完毕');
	//trace(event.result);
	//删除初始化屏幕 5
    /*this.initTotal -= 1;
    trace(this.initTotal);
	if(this.initTotal == 0){
		this.removeChild(this.mling);
	}*/
}
// for Stages HTTPService
private function BorderMenuRPCResult(event:ResultEvent):void
{
	borderXML = XML(event.result);
	trace('边框分类菜单加载完毕');
	
	//删除初始化屏幕 6
    /*this.initTotal -= 1;
    trace(this.initTotal);
	if(this.initTotal == 0){
		this.removeChild(this.mling);
	}*/
}
// for jewelry HTTPService
private function JewelryMenuRPCResult(event:ResultEvent):void
{
	jewelryXML = XML(event.result);
	trace('饰品分类菜单加载完毕');
	//删除初始化屏幕 7
    /*this.initTotal -= 1;
    trace(this.initTotal);
	if(this.initTotal == 0){
		this.removeChild(this.mling);
	}*/
}
// for calendar HTTPService
private function CalendarMenuRPCResult(event:ResultEvent):void
{
	calendarXML = XML(event.result);
	trace('日历分类菜单加载完毕');
	//trace(event.result);
	//删除初始化屏幕 8
    /*this.initTotal -= 1;
    trace(this.initTotal);
	if(this.initTotal == 0){
		this.removeChild(this.mling);
	}*/
}

/**
 * 点击左则素材子分类，HTTPService结果。
 */
private function SourceRPCResult(event:ResultEvent):void{
	sourceXML = XML(event.result);
	id_sources.dataProvider = sourceXML.info;
	trace('素材加载完毕');
	//trace(event.result);
}

/**
 * 保存边框的结果
 */
public function SaveBorderRPCResult(event:ResultEvent):void{
	var data1:XML = XML(event.result);
	var d1:String = data1.info.image;
	//trace(data1);
	if(d1 == 'NOOK'){
		Alert.show('系统忙，请稍后再试。');
		return;
	}
	//显示预览
	//ShowBorderDialog(data1.info);
		
	//canvas1(loadedSM.application).setMainImage(data1.info);
}

/**
 * 水平翻转
 */
public function FlipHorizontalRPCResult(event:ResultEvent):void{
	var data1:XML = XML(event.result);
	var d1:String = data1.info.image;
	//trace(d1);
	if(d1 == 'NOOK'){
		Alert.show('系统忙，请稍后再试。');
		return;
	}
	canvas1(loadedSM.application).changeImage1(data1.info);
	trace('水平翻转生成完毕');
}

/**
 * 垂直翻转
 */
public function FlipVerticalRPCResult(event:ResultEvent):void{
	var data1:XML = XML(event.result);
	var d1:String = data1.info.image;
	//trace(d1);
	if(d1 == 'NOOK'){
		Alert.show('系统忙，请稍后再试。');
		return;
	}
	canvas1(loadedSM.application).changeImage1(data1.info);
	trace('垂直翻转生成完毕');
}

/**
 * 合成场景的结果
 */
public function FinishStageRPCResult(event:ResultEvent):void{
	//trace(event.result);//return;
	var data1:XML = XML(event.result);
	var d1:String = data1.info.image;
	//trace(d1);
	if(d1 == 'NOOK'){
		Alert.show('系统忙，请稍后再试。');
		return;
	}
		
	canvas1(loadedSM.application).setMainImage(data1.info);
	//合成场景完毕，删除进度条
	if(this.ishowml){
		this.removeChild(this.mling);
		this.ishowml = false;
	}
}


//getSave1Result，保存 场景，边框，日历的设计
public function getSave1Result(e:ResultEvent):void{
	trace('保存设计1成功');
	//trace(e.result);
	var data:XML = XML(e.result);
	var d1:String = data.info.result;
	if(d1 == 'success'){
		
	}else if(d1 == 'failure'){
		Alert.show('系统忙，请稍后再试。');
		return;
	}
	this.SendHttpService(this.url_MyMakedRPC, null,this.MakedRPCResult, '完成的作品');
}

//删除我的照片
public function DelMyPhotoRPCResult(e:ResultEvent):void{
	trace('删除照片成功');
	//trace(e.result);
	var data:XML = XML(e.result);
	var d1:String = data.info.result;
	if(d1 == 'success'){
		
	}else if(d1 == 'failure'){
		Alert.show('系统忙，请稍后再试。');
		return;
	}
	this.SendHttpService(this.url_MyPhotoRPC, null,this.getMyPhotoRPCResult, '读取照片');
}
//删除设计完的照片
public function DelMyMakedRPCResult(e:ResultEvent):void{
	trace('删除设计完成的照片成功');
	//trace(e.result);
	var data:XML = XML(e.result);
	var d1:String = data.info.result;
	if(d1 == 'success'){
		
	}else if(d1 == 'failure'){
		Alert.show('系统忙，请稍后再试。');
		return;
	}
	this.SendHttpService(this.url_MyMakedRPC, null,this.MakedRPCResult, '读取完成的照片');
}

//取版本信息
public function getVersionRPCResult(e:ResultEvent):void{
	trace('取版本信息');
	//trace(e.result);
	var data:XML = XML(e.result);
	
	if(data == 'error'){
		var data1:XML = XML("<root><version>α</version><subver>β</subver><lastupdate>1985-05-15</lastupdate></root>");
		showVersion(data1);
	}else{
		showVersion(data);
	}
	
}

